package com.libs.utils;

import com.libs.sys.model.Userseat;
import com.libs.sys.service.LibService;
import com.libs.sys.service.impl.LibServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

@Slf4j
public class test {

    LibService libService = new LibServiceImpl();

    Userseat userseat = new Userseat("18037110", "070010", "100455730");
    public static void main(String[] args) throws Exception {
//        CloseableHttpClient httpClient = HttpClientConn.getHttpClient();
//        String url = "https://lib-seat.chu.edu.cn/ClientWeb/pro/page/image.aspx?";
//        CloseableHttpResponse getRes = HttpClientConn.getGetRes(url, true, httpClient);
//        FileUtils.copyToFile(getRes.getEntity().getContent(), new File("G:\\project\\libs\\src\\main\\resources\\img\\1.jpg"));
//
//        int num=0;
//        for (int i = 0; i < 10; i++) {
//            LibService libService = new com.libs.sys.service.impl.LibServiceImpl();
//            CloseableHttpClient httpClient = HttpClientConn.getHttpClient();
//            String name=libService.login(new Userseat("18037110","070010","100455730"),httpClient);
//
//            if(!("-1".equals(name))){
//                num++;
//            }
//        }
//        System.out.println(num);
//        LibService libService = new com.libs.sys.service.impl.LibServiceImpl();
//        CloseableHttpClient httpClient = HttpClientConn.getHttpClient();
//        Userseat userseat = new Userseat("18037110","070010","100455730");
//        Userseat userseat = new Userseat("18037105","200010","100455732");
//        Userseat userseat = new Userseat("18037105", "200010", "100456448");
//        System.out.println(libService.login(userseat, httpClient));
//        System.out.println((libService.setSeat2(userseat, httpClient, 2)));
//        libService.login(new Userseat("18037105","200010","100455732"),httpClient);
//        libService.signSeat(userseat, httpClient);
        final List<Userseat> userSeatList = UserSeatUtils.getUserSeatList();
        for (Userseat userseat:
             userSeatList) {
            t(userseat);
        }
    }

    static void t(Userseat userseat) throws Exception {
        LibService libService = new com.libs.sys.service.impl.LibServiceImpl();
        CloseableHttpClient httpClient = HttpClientConn.getHttpClient();
        boolean isLogin = false;
        String name = "";
        while (!isLogin) {
            name = libService.login(userseat, httpClient);
            isLogin = !"-1".equals(name);
        }
        char flag = libService.setSeat(userseat, httpClient);
        System.out.println(flag);
//        libService.signSeat(userseat,httpClient);
    }
    static void t1(Userseat userseat) throws Exception {
        final LibService libService = new LibServiceImpl();
//        Userseat userseat = new Userseat("18037110", "070010", "100455730");
        int num = 1;
        char flag = '0';
        final int FINISH_MINUTE = 59;
        final char REQUST = '0';
        String res = "";
        String name = "";
        boolean isLogin = false;
        boolean isSet = false;
        CloseableHttpClient httpClient = HttpClientConn.getHttpClient();
        while (!isLogin) {
            name = libService.login(userseat, httpClient);
            isLogin = !"-1".equals(name);
        }
        if(isLogin) {
            log.info("{}登录成功", name);
            while (Calendar.getInstance().get(Calendar.MINUTE) < 30 &&
                    Calendar.getInstance().get(Calendar.HOUR) == 5) {
            }
            long begin = System.currentTimeMillis();
            while (REQUST == flag) {
                flag = libService.setSeat(userseat, httpClient);
                num++;
            }
            long finish = System.currentTimeMillis();
            if (REQUST == flag) {
                res = "失败";
            } else {
                res = "成功";
            }
            log.info("【{}】抢座{}={}ms/次--{}次", name, res, (finish - begin) / num, num);
        }else{
            log.info("【{}】登录失败", userseat.getUid());
        }
    }
    static void t2() throws Exception {
        boolean fin = false;
        final LibService libService = new LibServiceImpl();
        Userseat userseat = new Userseat("18037110", "070010", "100455730");

        while (!fin) {
            long begin = System.currentTimeMillis();
            fin = libService.signSeat(userseat, HttpClientConn.getHttpClient());
            if (fin) {
                log.info(userseat.getUid() + "签到成功！");
            } else {
                log.info(userseat.getUid() + "签到失败！");
            }
            long finish = System.currentTimeMillis();
            log.info("{}签到耗时{}s", userseat.getUid(), (finish - begin) / 1000);
        }
        if (!fin) {
            log.info("{}没有签到成功！赶快签到！！！", userseat.getUid());
        } else {
            log.info("{}签到成功，安心睡觉啊", userseat.getUid());
        }
    }
}
